﻿using System;

public class Mapper068 : MapperDefault
{
    internal int r1;
    internal int r2;
    internal int r3;
    internal int r4;

    public override void loadROM(ROM rom)
    {
        if (rom.Valid)
        {
            int romBankCount = rom.RomBankCount;
            this.loadRomBank(0, 0x8000);
            this.loadRomBank(romBankCount - 1, 0xc000);
            this.loadCHRROM();
            this.loadBatteryRam();
            base.nes.Cpu.requestIrq(2);
        }
    }

    public override void reset()
    {
        this.r1 = this.r2 = this.r3 = this.r4 = 0;
    }

    private void setMirroring()
    {
        if (this.r1 == 0)
        {
            switch (this.r2)
            {
                case 0:
                    base.ppu.Mirroring = 1;
                    return;

                case 1:
                    base.ppu.Mirroring = 0;
                    return;

                case 2:
                    base.ppu.Mirroring = 3;
                    return;

                case 3:
                    base.ppu.Mirroring = 4;
                    return;
            }
        }
        else
        {
            switch (this.r2)
            {
            }
        }
    }

    public override void write(int address, short value_Renamed)
    {
        if (address < 0x8000)
        {
            base.write(address, value_Renamed);
        }
        else
        {
            switch ((address >> 12))
            {
                case 8:
                    this.load2kVromBank(value_Renamed, 0);
                    return;

                case 9:
                    this.load2kVromBank(value_Renamed, 0x800);
                    return;

                case 10:
                    this.load2kVromBank(value_Renamed, 0x1000);
                    return;

                case 11:
                    this.load2kVromBank(value_Renamed, 0x1800);
                    return;

                case 12:
                    this.r3 = value_Renamed;
                    this.setMirroring();
                    return;

                case 13:
                    this.r4 = value_Renamed;
                    this.setMirroring();
                    return;

                case 14:
                    this.r1 = (value_Renamed >> 4) & 1;
                    this.r2 = value_Renamed & 3;
                    this.setMirroring();
                    return;

                case 15:
                    this.loadRomBank(value_Renamed, 0x8000);
                    return;
            }
        }
    }
}

